package com.bright.controller.admin;


import com.bright.pojo.Type;
import com.bright.service.serviceimpl.TypeServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/admin")
public class TypeController {

    @Autowired
    private TypeServiceImpl typeService;

    @CrossOrigin
    @GetMapping("/types")
    public Page<Type> getallType(@PageableDefault(size = 8,sort = {"id"},direction = Sort.Direction.ASC) Pageable pageable){
       return typeService.listType(pageable);
    }

    @CrossOrigin
    @PostMapping("/type/input")
    public Type inputType(String typename){
        Type gettype = typeService.getByName(typename);
        if (gettype!=null){
            System.out.println("添加失败");
            return null;
        }
        Type t = new Type();
        t.setName(typename);
//        System.out.println(type);
        return typeService.saveType(t);
    }


    @CrossOrigin
    @PostMapping("/type/update")
    public Type updateType(long id,String typename){
        if (typeService.getByName(typename)==null){
            return null;
        }
        Type t = new Type();
        t.setId(id);
        t.setName(typename);
        return typeService.updataType(id,t); 
    }

    @CrossOrigin
    @PostMapping("/type/delete")
    public String deleteType(long id){
        typeService.deleteType(id);
        return "删除成功";
    }
}
